﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BGSpaceShooterServer;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input;

namespace BGSpaceShooter
{
    public class UserInterfaceController : IStatusObserver, IStateObserver
    {
        public UserInterface UserInterface { get; set; }

        public Camera Camera { get; set; }

        private GamePad GamePad { get; set; }

        public UserInterfaceController(Camera camera)
        {
            Camera = camera;
            UserInterface = new UserInterface();
        }

        public void StartGamePad(string ipAddress, int port)
        {
            GamePad = new GamePad(ipAddress, port);
            GamePad.Player = Camera.FollowedObject;
        }

        public void Stop()
        {
            if (GamePad != null)
            {
                GamePad.Stop = true;
            }
        }
        public void UpdateInput(IEnumerable<StarFighter> starFighters, IEnumerable<Projectile> projectiles)
        {
            if (UserInterface.CheckClicked(Keys.F6))
            {
                UserInterface.ToggleFullScreen(Camera.FollowedObject, starFighters, projectiles);
                UserInterface.PrepareMenu();
                UserInterface.PrepareServerChooseControls();
            }
            if (Router.GameState == GameState.Game)
            {
                if (UserInterface.CheckClicked(Keys.OemTilde))
                {
                    UserInterface.ChangeChatInputVisible();
                }
                if (UserInterface.IsChatActive())
                {
                    UserInterface.HandleChatInput(UserInterface.GetPressedKeys());
                }
                else
                {
                    if (UserInterface.GetPressedKeys().Contains(Keys.OemOpenBrackets))
                    {
                        UserInterface.RadarScale *= 0.9f;
                    }
                    if (UserInterface.GetPressedKeys().Contains(Keys.OemCloseBrackets))
                    {
                        UserInterface.RadarScale *= 1.1f;
                    }
                }

                UserInterface.UpdateClicked();
            }
        }

        public void UpdateUserInterface(IEnumerable<StarFighter> starFighters)
        {
            if (Router.GameState == GameState.Game)
            {
                UserInterface.UpdateControls(Camera.FollowedObject, starFighters);
            }
            else if (Router.GameState == GameState.Menu)
            {
                UserInterface.UpdateMenu();
            }
            else if(Router.GameState == GameState.ServerChoose)
            {
                UserInterface.UpdateServerChoose();
            }
        }

        private void UpdateCamera()
        {
            Camera.UpdateEngines(GetInputForServer());
            Camera.UpdateGravityInfo(GamePad);
            Camera.UpdateCamera();
        }

        public void SetActivePlayer(StarFighter player, IEnumerable<StarFighter> starFighters, IEnumerable<Projectile> projectiles)
        {
            Camera.FollowedObject = player;
            UserInterface.PrepareControls(player, starFighters, projectiles);
        }

        public List<Keys> GetInputForServer()
        {
            if (UserInterface.IsChatActive())
            {
                return new List<Keys>();
            }

            List<Keys> keysPressed = UserInterface.GetPressedKeys();
            keysPressed.AddRange(GamePad.getKeyAndAxisState());
            return keysPressed;
        }

        public IEnumerable<string> GetChatMessages()
        {
            return UserInterface.GetChatMessages();
        }

        #region IStatusObserver Members

        public void Update(ServerStatusResponse statusResponse)
        {
            //  Camera.UpdateEngines(statusResponse.Engines);
            //  Camera.UpdateEngineRotation(statusResponse.Velocity);
        }

        public void BeginUpdate()
        {

        }

        public void EndUpdate()
        {
            UpdateCamera();
        }
        #endregion

        public void Update(ServerStateResponse response)
        {
            if (response.EventType == EventType.ChatMessage)
            {
                UserInterface.AddNewChatMessage(response.Message);
            }
        }

        public List<IDrawer> GetDrawers(GameState gameState)
        {
            List<IDrawer> drawers = new List<IDrawer>();
            switch (gameState)
            {
                case GameState.Game:
                    drawers.Add(UserInterface.StarSphere.GetDrawer());
                    drawers.Add(UserInterface.GetDrawer());
                    break;
                case GameState.Menu:
                    drawers.Add(UserInterface.GetMenuDrawer());
                    break;
                case GameState.ServerChoose:
                    drawers.Add(UserInterface.GetServerChooseDrawer());
                    break;
                case GameState.Initialize:
                    drawers.Add(UserInterface.GetWaitDrawer());
                    break;
            }
            return drawers;
        }
    }
}
